﻿using IDal;

namespace Dal
{
    public class BaseDal<T> : IBaseDal<T> where T : class
    {
        public BaseDal(MyDbContext context)
        {
            Context = context;
        }

        public MyDbContext Context { get; }

        public async Task<int> AddAsync(T t)
        {
            await Context.Set<T>().AddAsync(t);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> AddRangeAsync(List<T> list)
        {
            await Context.Set<T>().AddRangeAsync(list);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> DelAsync(T t)
        {
            Context.Set<T>().Remove(t);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> DelRangeAsync(List<T> list)
        {
            Context.Set<T>().RemoveRange(list);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> UpdaAsync(T t)
        {
            Context.Set<T>().Update(t);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> UpdaRangeAsync(List<T> list)
        {
            Context.Set<T>().UpdateRange(list);
            return await Context.SaveChangesAsync();
        }

        public IQueryable<T> GetAsync()
        {
            return Context.Set<T>();
        }
    }
}
